﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Modules.Core;

namespace ComputerStoreManager.Controls {
    /// <summary>
    /// Interaction logic for ProductItem.xaml
    /// </summary>
    public delegate void OnProductEventHandler(double e, string maMH, string giaBan, string soLuongBan);
    public partial class ProductItem : UserControl {
        public ICommand SelectionChangedCommand {
            get;
            private set;
        }
        public event OnProductEventHandler OnOkEventHandler;
        private DataTable listMatHang;
        public double TotalPrice {
            get;
            set;
        }
        public ProductItem() {
            InitializeComponent();
            SelectionChangedCommand = new ExecutableCommand(SelectionChangedEvent);
            listMatHang = Model.GetListLinhKien();
            dataGrid.ItemsSource = listMatHang.DefaultView;
        }

        private void SelectionChangedEvent(object obj) {
            if (dataGrid.Items != null && dataGrid.Items.Count > 0 && listMatHang.Rows.Count > 0) {
                txtProduct.Text = listMatHang.Rows[dataGrid.SelectedIndex]["MaMH"].ToString();
                finallyProduct.Text = listMatHang.Rows[dataGrid.SelectedIndex]["MaMH"].ToString();
                txtSalePrice.Text = listMatHang.Rows[dataGrid.SelectedIndex]["GiaBan"].ToString();
                txtNumber.Text  = listMatHang.Rows[dataGrid.SelectedIndex]["SoLuongTonKho"].ToString();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            //search
            try {
                string str = txtSearching.Text;
                var dt = Model.SearchInfo(str, (Modules.Core.SearchingCondition)cbbCondition.SelectedIndex, Modules.Core.KindOfObject.BY_PRODUCT);
                if (dt != null && dt.Rows.Count > 0) {
                    dataGrid.ItemsSource = dt.DefaultView;
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi khi tìm dữ liệu" + ex.Message);
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e) {
            //ok
            gridSearchMatHang.Visibility = Visibility.Collapsed;
            txtProduct.Text = finallyProduct.Text;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e) {
            if (this.gridSearchMatHang.Visibility == Visibility.Visible) {
                this.gridSearchMatHang.Visibility = Visibility.Collapsed;
            }
            else {
                this.gridSearchMatHang.Visibility = Visibility.Visible;
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e) {
            //ok button
            if (txtNumber.Text != string.Empty && txtSalePrice.Text != string.Empty && txtProduct.Text != string.Empty) {
                if(int.Parse(txtNumber.Text) > int.Parse(listMatHang.Rows[dataGrid.SelectedIndex]["SoLuongTonKho"].ToString())) {
                    MessageBox.Show("Vượt quá hàng tồn kho");
                    return;
                }
                double price = int.Parse(txtNumber.Text) * double.Parse(txtSalePrice.Text);
                TotalPrice = price;
                OnProductEventHandler handler = OnOkEventHandler;
                if (handler != null) {
                    handler(TotalPrice,finallyProduct.Text,txtSalePrice.Text,txtNumber.Text);
                }

                btnFinalOk.IsEnabled = false;
                btnSearch.IsEnabled = false;
                txtSalePrice.IsEnabled = false;
                txtNumber.IsEnabled = false;
            } else {
                MessageBox.Show("Chưa nhập");
            }
        }

        private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            if (dataGrid.Items != null && dataGrid.Items.Count > 0 && listMatHang.Rows.Count > 0) {
                txtProduct.Text = listMatHang.Rows[dataGrid.SelectedIndex]["MaMH"].ToString();
                finallyProduct.Text = listMatHang.Rows[dataGrid.SelectedIndex]["MaMH"].ToString();
                txtSalePrice.Text = listMatHang.Rows[dataGrid.SelectedIndex]["GiaBan"].ToString();
                txtNumber.Text = listMatHang.Rows[dataGrid.SelectedIndex]["SoLuongTonKho"].ToString();
            }
        }
    }
}
